package com.chen.learnknowledge.controller;


import com.chen.learnknowledge.entity.User;
import com.chen.learnknowledge.service.UserService;
import com.chen.learnknowledge.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/es")
public class TestEsController {

    @Autowired
    UserService userService;

    /**
     * 查出所有user中的数据，并放放入es当中保存
     */
    @PutMapping("/saveUsersToEs")
    public R getUserAllAndInsertIntoEs() throws IOException {

        //调用方法批量保存user数据到es
        Boolean isRight = userService.getUserAllAndInsertIntoEs();
        //isRight == flase 代表插入es成功
        if (isRight){
            return R.error();
        }
        return R.ok();
    }

    /**
     * 批量删除es
     * @param ids
     * @return
     * @throws IOException
     */
    @DeleteMapping("/bulkDeleteEs")
    public R bulkDeleteEs(@RequestParam("ids") List<Integer> ids) throws IOException {

        userService.bulkDeleteEs(ids);
        return R.ok();
    }


    /**
     * 接收从前台传来的user，插入数据库并放放入es当中保存
     */
    @PostMapping("/saveUserToEs")
    public R saveUserToEs(@RequestBody User user) throws Exception {

        if (StringUtils.isEmpty(user)){
            throw new Exception("user为空");
        }
        userService.saveUserToEs(user);
        return R.ok();
    }

    /**
     * 收从前台传来的user，修改数据库并放放入es当中保存
     */
    @PutMapping("/updateUserAndEs")
    public R updateUserAndEs(@RequestBody User user) throws Exception {

        userService.updateUserAndEs(user);
        return R.ok();
    }

    /**
     * 根据id查询es中数据
     */
    @GetMapping("/getEsById")
    public R getEsById(@RequestParam("id") Integer id) throws Exception {

        User user = userService.getEsById(id);
        return R.ok().put("user",user);
    }

    /**
     * 根据id删除es中数据
     */
    @DeleteMapping ("/deleteEsById")
    public R deleteEsById(@RequestParam("id") Integer id) throws Exception {

        userService.deleteEsById(id);
        return R.ok();
    }

    /**
     * 查询es中所有数据
     */
    @GetMapping("/getAllFromEs")
    public R getAllFromEs() throws IOException {
        List<User> userList = userService.getAllFromEs();
        return R.ok().put("users",userList);
    }

    /**
     * 条件查询es中所有数据
     */
    @GetMapping("/getUserFromEsByUserElement")
    public R getUserFromEsByUserElement() throws IOException {

        List<User> userList = userService.getUserFromEsByUserElement();

        return R.ok().put("users",userList);
    }

    /**
     * es分页查询
     */
    @GetMapping("/getUserFromEsWithPage")
    public R getUserFromEsWithPage(@RequestParam("pageNum") Integer pageNum,
                                   @RequestParam("pageSzie") Integer pageSize) throws IOException {

        List<User> userList = userService.getUserFromEsWithPage(pageNum,pageSize);

        return R.ok().put("users",userList);
    }

    /**
     * 查询排序
     */
    @GetMapping("/getUserFromEsOrderByAge")
    public R getUserFromEsOrderByAge() throws IOException {

        List<User> userList = userService.getUserFromEsOrderByAge();

        return R.ok().put("users",userList);
    }

    /**
     * 组合查询
     */
    @GetMapping("/getUserFromEsByElements")
    public R getUserFromEsByElements() throws IOException {

        List<User> userList = userService.getUserFromEsByElements();

        return R.ok().put("users",userList);
    }

    /**
     * 范围查询
     */
    @GetMapping("/getUserFromEsByRange")
    public R getUserFromEsByRange() throws IOException {

        List<User> userList = userService.getUserFromEsByRange();

        return R.ok().put("users",userList);
    }

    /**
     * 模糊查询
     */
    @GetMapping("/getUserFromEsByFuzzyQuery")
    public R getUserFromEsByFuzzyQuery() throws IOException {

        List<User> userList = userService.getUserFromEsByFuzzyQuery();

        return R.ok().put("users",userList);
    }


    /**
     * 最大值查询
     */
    @GetMapping("/getUserFromEsByMaxValue")
    public R getUserFromEsByMaxValue() throws IOException {

        String maxValue = userService.getUserFromEsByMaxValue();

        return R.ok().put("maxValue",maxValue);
    }



}
